﻿using BLL.AntiFake;
using Model;
using Model.AntiFake;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Common;
using BLL.Organization;
namespace DAGApp
{
    public partial class fw : System.Web.UI.Page
    {
        QueryCodeLogBLL bll = new QueryCodeLogBLL();
        EnterpriseBLL eBll = new EnterpriseBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                string fw_code = Request.QueryString["code"];//获取url路径的防伪码信息  
                if(!string.IsNullOrEmpty(fw_code)){//不为空
                  
                    string TableName;
                    bool flag = bll.TabIsExist(fw_code,out TableName);
                    string strResult;
                    if (flag)
                    {//表存在
                        //查询真伪
                        AntiFake_DigitcodeModel DigitcodeModel = bll.GetModelByFw_Code(fw_code);
                        string msg;
                       
                        if (DigitcodeModel != null)
                        {//码是否存在
                           //更改查询次数 第一次查询时 更改查询次数和时间 
                            if (DigitcodeModel.QueryCount==0)
                            {
                                DigitcodeModel.QueryCount = 1;
                                DigitcodeModel.QueryTime = DateTime.Now;
                            }
                            else
                            {
                                DigitcodeModel.QueryCount++;
                            }

                            bll.UpdateDigitcode(DigitcodeModel, TableName);
                            msg = "正品！";
                         //  strResult = "<p style=\"padding-bottom: 0.8rem;\">人工防伪查询热线:400-0503-315</p><p>产品名:*********</p> <p>产品名称:*********</p> <p>查询次数:" + DigitcodeModel.QueryCount + "</p>";

                            strResult = "<p style=\"padding-bottom: 0.25rem;\">查询记录【" + DigitcodeModel.Fw_Code + "】</p>  <div style='background-color:#f7f7f7;padding-top:15px;padding-bottom:10px;padding-left:5px;margin-bottom:25px'> <p> &nbsp &nbsp 您好，你所查的是国家档案局所监管的产品！是正牌产品的标识，欢迎您再次选购！</p></div><p>企业名称:" + DigitcodeModel.EnterpriseName + "</p> <p style='float:left'>查询次数:第" + DigitcodeModel.QueryCount + "次查询</p>";
                        }

                        else
                        {
                            strResult = "<p style=\"padding-bottom: 0.8rem;\">该防伪码不存在，请谨防假冒</p>";
                            msg = "该防伪码不存在，请谨防假冒";
                        }
                        //获取机构id
                        string enterCode = MiMa.Jiemi(fw_code, AppConst.DigitCodeKey).Substring(0,4);
                     
                        //生成记录
                        QueryCodeLogModel model = new QueryCodeLogModel();
                        IPMethod ipMethod = new IPMethod();
                        model.Fw_Code = DigitcodeModel.Fw_Code;
                        model.QueryType = "4";//查询方式
                        model.QueryValue = ipMethod.GetHostAddress(); 
                        model.QueryTime = DateTime.Now;
                        var addre=ipMethod.IPGetCity( model.QueryValue) ;
                        //var addre = ipMethod.IPGetCity("218.205.64.194");
                        model.QueryProvince = addre.Split('-')[0];
                        model.QueryCity = addre.Split('-')[1]; ;
                        model.ProductId = 1;
                        model.Valid = true;
                        model.EnterpriseId = eBll.GetModelByCode(PageValidate.ConvertToTrimInt(enterCode)).ID;  
                        model.QueryMessage = msg;
                        ResultEntity entity = bll.AddQueryCodeLog(model);
                        this.resultContent.InnerHtml = strResult;
                    }
                    else//表不存在   跳转到错误界面
                        System.Web.HttpContext.Current.Response.Write(String.Format("<script language=\"javascript\">window.location.replace(\"{0}\")</script>", "Error.aspx"));

                    
                }
            }
        }
    }
}